package org.jeecg.modules.procurement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.procurement.entity.BussMaterialApplication;
import org.jeecg.modules.procurement.entity.BussMaterialApplicationDetail;
import org.jeecg.modules.procurement.entity.BussMaterialProcurementPlanDetail;
import org.jeecg.modules.procurement.mapper.BussMaterialApplicationDetailMapper;
import org.jeecg.modules.procurement.mapper.BussMaterialApplicationMapper;
import org.jeecg.modules.procurement.mapper.BussMaterialProcurementPlanDetailMapper;
import org.jeecg.modules.procurement.service.IBussMaterialApplicationService;
import org.jeecg.modules.procurement.vo.BussMaterialApplicationPageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 材料请购单
 * @Author: jeecg-boot
 * @Date:   2024-06-05
 * @Version: V1.0
 */
@Service
public class BussMaterialApplicationServiceImpl extends ServiceImpl<BussMaterialApplicationMapper, BussMaterialApplication> implements IBussMaterialApplicationService {

	@Autowired
	private BussMaterialApplicationMapper bussMaterialApplicationMapper;
	@Autowired
	private BussMaterialApplicationDetailMapper bussMaterialApplicationDetailMapper;

	@Autowired
	private BussMaterialProcurementPlanDetailMapper bussMaterialProcurementPlanDetailMapper;

	@Override
	public IPage<BussMaterialApplicationPageVo> pagelist(Page<BussMaterialApplicationPageVo> page, QueryWrapper<BussMaterialApplicationPageVo> queryWrapper) {
		return this.baseMapper.pagelist(page,queryWrapper);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(BussMaterialApplication bussMaterialApplication, List<BussMaterialApplicationDetail> bussMaterialApplicationDetailList) {
		bussMaterialApplicationMapper.insert(bussMaterialApplication);
		if(bussMaterialApplicationDetailList!=null && bussMaterialApplicationDetailList.size()>0) {
			for(BussMaterialApplicationDetail entity:bussMaterialApplicationDetailList) {
				//外键设置
				entity.setApplicationId(bussMaterialApplication.getId());
				entity.setApplicationCode(bussMaterialApplication.getCode());
				bussMaterialApplicationDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(BussMaterialApplication bussMaterialApplication,List<BussMaterialApplicationDetail> bussMaterialApplicationDetailList) {
		bussMaterialApplicationMapper.updateById(bussMaterialApplication);
		
		//1.先删除子表数据
		bussMaterialApplicationDetailMapper.deleteByMainId(bussMaterialApplication.getId());
		
		//2.子表数据重新插入
		if(bussMaterialApplicationDetailList!=null && bussMaterialApplicationDetailList.size()>0) {
			for(BussMaterialApplicationDetail entity:bussMaterialApplicationDetailList) {
				//外键设置
				entity.setApplicationId(bussMaterialApplication.getId());
				bussMaterialApplicationDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
//		bussMaterialApplicationDetailMapper.deleteByMainId(id);
//		bussMaterialApplicationMapper.deleteById(id);
		List<BussMaterialApplicationDetail> bussMaterialApplicationDetails = bussMaterialApplicationDetailMapper.selectList(new LambdaQueryWrapper<BussMaterialApplicationDetail>()
				.eq(BussMaterialApplicationDetail::getApplicationId, id).select(BussMaterialApplicationDetail::getId));
		List<String> strlist = bussMaterialApplicationDetails.stream().map(BussMaterialApplicationDetail::getId).collect(Collectors.toList());
		this.delBatchMain(strlist);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
//			bussMaterialApplicationDetailMapper.deleteByMainId(id.toString());
//			bussMaterialApplicationMapper.deleteById(id);
			this.deleteData((String) id);
		}
	}


	private void deleteData(String id){
		BussMaterialApplicationDetail bussMaterialApplicationDetail = bussMaterialApplicationDetailMapper.selectById(id);
		Long planDetCount = bussMaterialProcurementPlanDetailMapper.selectCount(new LambdaQueryWrapper<BussMaterialProcurementPlanDetail>()
				.eq(BussMaterialProcurementPlanDetail::getApplicationDetailId, id));
		if(planDetCount > 0){
			throw new RuntimeException("已制作计划单，无法删除");
		}
		bussMaterialApplicationDetailMapper.deleteById(id);
		Long detCount = bussMaterialApplicationDetailMapper.selectCount(new LambdaQueryWrapper<BussMaterialApplicationDetail>()
				.eq(BussMaterialApplicationDetail::getApplicationId, bussMaterialApplicationDetail.getApplicationId()));
		if(detCount == 0){
			bussMaterialApplicationMapper.deleteById(bussMaterialApplicationDetail.getApplicationId());
		}

	}

	
}
